package com.zhuss.third;

import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;

import com.zhuss.exception.OceanException;
import com.zhuss.util.JsonUtil;

/**
 * @Author: zhushanshuo
 * @Date: 2020/04/21 19:17
 */
@Component
public class ExampleThirdService {
	
	private static String url = "https://myvx.zhuss.cn:4433//pc/part/list";
	
	private static String jsonString = JsonUtil.toJson(new ExParam());
	
	
	public static void main(String[] args) throws Exception {
		Object response = query(url, jsonString, "0df6d017-c2dc-477c-9824-04d5801fba93");
		System.out.println(JsonUtil.toJson(response));
	}
    
    
    public Object query(String token) throws OceanException {
    	Object response = null;
    	try {
			response = query(url, jsonString, token);
		} catch (Exception e) {
			e.printStackTrace();
			// throw new OceanException(ExceptionCode.HK接口失败);
		}
    	return response;
    }
    
    public static Object query(String url, String jsonString, String token) throws Exception {
        CloseableHttpClient hc = null;
        try {
            hc = HttpClients.createDefault();
            HttpPost post = new HttpPost(url);
            
            Header header2 = new BasicHeader("token", token);
            Header[] headers = new Header[]{header2};
            post.setHeaders(headers);
            
            HttpEntity entity = new StringEntity(jsonString, ContentType.APPLICATION_JSON);
            post.setEntity(entity);  
            
            HttpResponse response = hc.execute(post);
            if (response.getStatusLine().getStatusCode() != 200) {
                throw new Exception();
            }

            HttpEntity httpEntity = response.getEntity();
            String result = EntityUtils.toString(httpEntity);

            Temp result2 = JsonUtil.fromJson(result, Temp.class);
            if (result2 == null || result2.getCode() != 0) {
                throw new Exception();
            }
            
            return result2;

        } catch (Exception e) {
            throw new Exception();
        } finally {
            try {
                if (hc != null) {
                    hc.close();
                }
            } catch (Exception e2) {
            }
        }
    }
    
}

class ExParam {
	Integer pageSize = 10;
	
	Integer pageNum = 1;
	
	public Integer getPageSize() {
		return pageSize;
	}
	
	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}
	
	public Integer getPageNum() {
		return pageNum;
	}
	
	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}
}

class ExTemp {
	Integer code;
	
	String msg;
	
	Temp1 data;

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Temp1 getData() {
		return data;
	}

	public void setData(Temp1 data) {
		this.data = data;
	}
	
}

class ExTemp1 {
	Temp2 data;

	public Temp2 getData() {
		return data;
	}

	public void setData(Temp2 data) {
		this.data = data;
	}
}

class ExTemp2 {
	Object items;
	
	Integer pageSize;
	
	Integer total;
	
	Integer pageNum;
	
	Integer totalPage;

	public Object getItems() {
		return items;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	
}

class Temp {
	Integer code;
	
	String msg;
	
	Temp1 data;

	public Integer getCode() {
		return code;
	}

	public void setCode(Integer code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public Temp1 getData() {
		return data;
	}

	public void setData(Temp1 data) {
		this.data = data;
	}
	
}

class Temp1 {
	Temp2 data;

	public Temp2 getData() {
		return data;
	}

	public void setData(Temp2 data) {
		this.data = data;
	}
}

class Temp2 {
	Object items;
	
	Integer pageSize;
	
	Integer total;
	
	Integer pageNum;
	
	Integer totalPage;

	public Object getItems() {
		return items;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public Integer getPageSize() {
		return pageSize;
	}

	public void setPageSize(Integer pageSize) {
		this.pageSize = pageSize;
	}

	public Integer getTotal() {
		return total;
	}

	public void setTotal(Integer total) {
		this.total = total;
	}

	public Integer getPageNum() {
		return pageNum;
	}

	public void setPageNum(Integer pageNum) {
		this.pageNum = pageNum;
	}

	public Integer getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(Integer totalPage) {
		this.totalPage = totalPage;
	}
	
}
